<?php

namespace app\api\model\service;

use think\Model;
class PlusPay extends Model
{
    // 表名
    protected $name = 'service_plus_pay';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public static function handlePlus($id)
    {
        $info = self::where(['id'=>$id,'is_update'=>0,'state'=>1])->field('id,user_id,plus_id')->find();
        if(!$info)
        {
            return false;
        }
        $plusConfig = PlusConfig::where(['id'=>$info['plus_id']])->field('name,daynums,discount')->find();
        if(!$plusConfig)
        {
            return false;
        }
        $userInfo = UserInfo::where(['user_id'=>$info['user_id']])->field('is_plus,discount,plusname,plustime')->find();
        if($userInfo['is_plus'] == 0){
            $data['is_plus'] = 1;
            $data['plustime'] = time()+86400*$plusConfig['daynums'];
        }else{
            $data['plustime'] = $userInfo['plustime']+86400*$plusConfig['daynums'];
        }
        if($userInfo['discount']>$plusConfig['discount'])
        {
            $data['discount'] = $plusConfig['discount'];
            $data['plusname'] = $plusConfig['name'];
        }
        UserInfo::where(['user_id'=>$info['user_id']])->update($data);
        return true;
    }
}
